function [all_fbpert,all_pbpert]=pert_pop_multiTime(ExpGroup,draw_exps)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   mean pert (local and full field) population response over several experimental
%   sessions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tmp_data_dir = '\\KELLER-RIG1-ANA\RawData\';
adata_dir = '\\argon.fmi.ch\keller.g\AData\';
fb_av={};
pb_av={};
for fnd=1:length(ExpGroup)
    display(['Exp: ' num2str(ExpGroup(fnd)) ' *****************************']);
    [adata_file,mouse_id,userID]=find_adata_file(ExpGroup(fnd),adata_dir);
    orig=load([adata_dir userID '\' mouse_id '\' adata_file],'ROIs','nbr_frames','aux_files');
    aux_data=[];
    try
        for ind=1:length(orig.aux_files)
            if exist([tmp_data_dir userID '\' mouse_id '\' orig.aux_files{ind}],'file');
                disp(['loading file ' orig.aux_files{ind} ' from local tmpdata directory']);
                curr_load_path=[tmp_data_dir userID '\' mouse_id '\' orig.aux_files{ind}];
            else
                disp(['loading file ' orig.aux_files{ind} ' from the network RawData directory']);
                curr_load_path=[rdata_dir userID '\' mouse_id '\' orig.aux_files{ind}];
            end
            tmp_data=load_lvd(curr_load_path);
            tmp_frames=get_frame_times(tmp_data(2,:));

            if orig.nbr_frames(ind)~=length(tmp_frames)
                if max(diff(tmp_frames))/median(diff(tmp_frames))<1.5 & length(tmp_frames)>orig.nbr_frames(ind)
                    disp('NC WARNING! Probably scanning too fast for FPGA to stop in the same frame')
                    disp(['--- ' num2str(length(tmp_frames)-orig.nbr_frames(ind)) ' frames too many ---'])
                    tmp_data(2,tmp_frames(orig.nbr_frames(ind)+1):end)=0;
                else
                    disp('CRITICAL WARNING! - aux data longer than it should be.')
                    %return
                    %tmp_data=tmp_data(:,1:tmp_frames(nbr_frames(ind)+1));
                    [~,last_bad_frame]=max(diff(tmp_frames));
                    tmp_data=tmp_data(:,ceil(mean([tmp_frames(last_bad_frame) tmp_frames(last_bad_frame+1)])):end);
                end
            end
            aux_data(:,end+1:end+length(tmp_data))=tmp_data;
        end
        
        frame_times=get_frame_times(aux_data(2,:));
        
%         vars_to_assigin_base={vars_to_assigin_base{:}, 'aux_data', 'frame_times'};
    catch
        disp('Did not load aux data')
    end
    [fb_av{fnd} pb_av{fnd}]=pert_pop(aux_data,frame_times,orig.ROIs,orig.nbr_frames,0,0,0);
end

curr_max=0;
for hnd=1:size(fb_av,2)*2
    if ~(hnd/(size(fb_av,2)*2)>0.5)
        for gnd=1:size(fb_av{hnd},2)
            curr_max=max([curr_max max(fb_av{hnd}{gnd})]);
        end
    else
        for gnd=1:size(pb_av{hnd-size(fb_av,2)},2)
            curr_max=max([curr_max max(pb_av{hnd-size(fb_av,2)}{gnd})]);
        end
    end
end

if draw_exps==1
    fig_pos=[520   431   720   367];
    for pnd=1:size(fb_av,2)
        figure;%('color',[1 1 1]);
        set(gcf,'position',fig_pos);
        for ind=1:size(fb_av{pnd},2)
            subplot(2,7,sequ(ind))
            rectangle('position',[1500 0.5 1000 4],'facecolor',[0.95 0.95 0.95],'linestyle','none')
            if ~isempty(fb_av{pnd}{ind})
                hold on
                plot(fb_av{pnd}{ind},'color',[1 0 0],'LineWidth',2)
            end
            if ~isempty(pb_av{pnd}{ind})
                hold on
                plot(pb_av{pnd}{ind},'color',[0 0 0],'LineWidth',2)
            end
            axis tight
            axis off
            box off
            ylim([0.9 curr_max])
            xlim([0 4001])
            drawnow
        end
    end
end

all_fbpert={};
all_pbpert={};
for fnd=1:size(fb_av{1},2)
    for ynd=1:size(fb_av,2)
        if ~isempty(fb_av{ynd}{fnd})
            all_fbpert{fnd}(:,ynd)=fb_av{ynd}{fnd};
        else
            all_fbpert{fnd}(:,ynd)=zeros(4001,1);
        end
        if ~isempty(pb_av{ynd}{fnd})
            all_pbpert{fnd}(:,ynd)=pb_av{ynd}{fnd};
        else
            all_pbpert{fnd}(:,ynd)=zeros(4001,1);
        end
    end
end

for vnd=1:size(fb_av,2)
    lines(vnd,:)=[0.9 0.9 0.9]-((0.9/(size(fb_av,2)+2))*vnd);
end

leg_list={};
for vnd=1:size(fb_av,2)
    leg_list{vnd}=['T' num2str(vnd-1)];
end
sequ=[1 2 3 8 9 10 4 5 6 11 12 13 7 14];
figure;
set(gcf,'position',[281         242        1190         556]);
for lnd=1:size(all_fbpert,2)
    subplot(2,7,sequ(lnd))
    set(gca,'XTick',[0 4000])
    rectangle('position',[1500 0.5 1000 4],'facecolor',[1 0.9 0.9],'linestyle','none')
    hold on
    for ynd=1:size(all_fbpert{lnd},2)
        plot(all_fbpert{lnd}(:,ynd),'color',lines(ynd,:))
    end
    ylim([0.9 curr_max])
    xlim([0 4001])
end
subplot(2,7,sequ(end))
hold on
for ynd=1:size(all_fbpert{lnd},2)
    plot(1,'color',lines(ynd,:))
end
box off
legend(leg_list)
axis off
box off

figure;
set(gcf,'position',[281         242        1190         556]);
for lnd=1:size(all_pbpert,2)
    subplot(2,7,sequ(lnd))
    set(gca,'XTick',[0 4000])
    rectangle('position',[1500 0.5 1000 4],'facecolor',[1 0.9 0.9],'linestyle','none')
    hold on
    for ynd=1:size(all_pbpert{lnd},2)
        plot(all_pbpert{lnd}(:,ynd),'color',lines(ynd,:))
    end
    ylim([0.9 curr_max])
    xlim([0 4001])
end
subplot(2,7,sequ(end))
hold on
for ynd=1:size(all_fbpert{lnd},2)
    plot(1,'color',lines(ynd,:))
end
box off
axis off
legend(leg_list)
axis off
box off